import { Plane, Sphere, Ray3, IntersectResult } from "@script/index";

export default class Union {
  private geometries: Array<Plane | Sphere>;

  constructor(geometries: Array<Plane | Sphere>) {
    this.geometries = geometries;
  }

  initialize(): void {
    this.geometries.forEach((item: Plane | Sphere) => {
      item.initialize();
    });
  }

  intersect(ray: Ray3): IntersectResult {
    let min_distance = Infinity;
    let min_result = IntersectResult.noHit;
    this.geometries.forEach((item: Plane | Sphere) => {
      let result = item.intersect(ray);
      if (result.geometry && result.distance < min_distance) {
        min_distance = result.distance;
        min_result = result;
      }
    });
    return min_result;
  }

}